package fm.dongman.modellib.source;

import android.support.annotation.NonNull;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import fm.dongman.animefm.contractlib.BaseDataSource;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.SheetListContract;
import fm.dongman.animefm.contractlib.model.SearchSheetResultModel;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.remote.RemoteOperationHelper;

/**
 * Created by KUIGE on 2017/10/28.
 */

public class SheetListRepository implements SheetListContract.ISheetListDataSource {
    private static final int mSize = 10; // 每页的数据量
    public static final String SPEC = "topic"; // 文章
    public static final String MANDAN = "mandan"; // 漫单
    public static final String USER_CREATE = "created_mandan"; // 用户创建的漫单
    public static final String USER_FOLLOW = "followed_mandan"; //  用户关注的漫单
    public static final String USER_COL = "collected_topic"; //  用户收藏的漫单

    private static SheetListRepository INSTANCE;

    private RemoteOperationHelper mOperationHelper;

    private int mPage;
    private int mOrder;
    private String mType;
    private String mId;

    public static SheetListRepository getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new SheetListRepository();
        }
        return INSTANCE;
    }

    private SheetListRepository() {
        mOperationHelper = RemoteOperationHelper.getOperationHelper();
    }

    @Override
    public void setSheetListInfo(int order, int page, @NonNull String type, String id) {
        mOrder = order;
        mPage = page;
        mType = type;
        mId = id;
    }

    @Override
    public void searchSheets(@NotNull String key, @NotNull LoadSourceCallback<? super SearchSheetResultModel> callback) {
            // 创建搜索结果对象
            SearchSheetResultModel resultModel = new SearchSheetResultModel();
            // 解析漫单
            JSONObject sheetJson = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.SEARCH_Q + "q=" + key + "&page=" + mPage + "&size=" + mSize + "&type=mandan");
            if (sheetJson != null) {
                resultModel.setMSheetResultList(mOperationHelper.downSearchSheet(sheetJson));
            }
            // 解析专题
            JSONObject topicJson = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.SEARCH_Q + "q=" + key + "&page=" + mPage + "&size=" + mSize + "&type=topic");
            if (topicJson != null) {
                resultModel.setMTopicResultList(mOperationHelper.downQTopicListData(topicJson));
            }
            // 都没有解析成功则回调失败
            if (sheetJson == null && topicJson == null) {
                callback.onDataLoadFiled();
                return;
            }
            // 最后解析成功回调
            callback.onDataLoaded(resultModel);
    }

    @Override
    public void getData(@NonNull BaseDataSource.LoadSourceCallback<? super List<? extends IModel.ISheetModel>> callback) {
            // 解析相关类型的 漫单专题列表
            if (mType.equals(SPEC) || mType.equals(MANDAN)) {
                parseSheetList(callback, mType);
                return;
            }
            // 解析用户信息相关类型的 漫单专题列表
            parseUserSheetList(callback, mType);
    }

    @Override
    public void deleteSheet(String s, LoadSourceCallback<? super String> loadSourceCallback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("session_key", StaticUrl.TOKEN_KEY);
        map.put("mandan_id", s);
        map.put("type","delete");
        JSONObject jsonObject = RemoteHelper.Companion.getInstance().parsePostUrl("http://api.dongman.fm/mandan/createOrEditMandan", map);
        if (jsonObject == null) {
            loadSourceCallback.onDataLoadFiled();
            return;
        }
        loadSourceCallback.onDataLoaded("");
    }

    /**
     * 解析用户信息相关的 漫单/专题列表
     *
     * @param callback 回调接口
     * @param type     类型
     */
    private void parseUserSheetList(@NonNull LoadSourceCallback<? super List<? extends IModel.ISheetModel>> callback, String type) {
        // 用户关注和收藏的漫单/专题没有页数
        if (mPage != 1) {
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("session_key", StaticUrl.TOKEN_KEY);
        map.put("type", type);
        if (mId != null && !mId.isEmpty()) {
            map.put("id", mId);
        }
        JSONObject jsonObject = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.USER_CREATE, map);
        if (jsonObject == null) {
            callback.onDataLoadFiled();
            return;
        }
        // 解析
        switch (type) {
            case USER_CREATE:
                callback.onDataLoaded(mOperationHelper.downUserCreateSheets(jsonObject));
                break;
            case USER_FOLLOW:
                callback.onDataLoaded(mOperationHelper.downUserFollowSheets(jsonObject));
                break;
            case USER_COL:
                callback.onDataLoaded(mOperationHelper.downUserCollectionTopics(jsonObject));
                break;
        }
    }

    /**
     * 解析相关类型的 漫单/专题列表
     *
     * @param callback 回调接口
     * @param type     类型
     */
    private void parseSheetList(@NonNull LoadSourceCallback<? super List<? extends IModel.ISheetModel>> callback, String type) {
        JSONObject jsonObject = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.SheetList + "type=" + type + "&page=" + mPage + "&size=" + mSize);
        if (jsonObject == null) {
            callback.onDataLoadFiled();
            return;
        }
        // 解析
        switch (type) {
            case SPEC:
                // 还有请求专题标签的没写
                callback.onDataLoaded(mOperationHelper.downQTopicListData(jsonObject));
                break;
            case MANDAN:
                callback.onDataLoaded(mOperationHelper.downSheetListData(jsonObject));
                break;
        }
    }

    @Override
    public void saveData(List<? extends IModel.ISheetModel> dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
    }
}